#include <stdio.h>
#include "common_list.h"

struct ListNode* swapPairs(struct ListNode* head) {
    if (!head || !(head->next))
        return head;

    struct ListNode *n1, *tmp;
    struct ListNode *tmp_h = (struct ListNode *)malloc(sizeof(*head));
    tmp_h->next = head;

    n1 = tmp_h;
    while (n1->next && n1->next->next) {
        tmp = n1->next;
        n1->next = n1->next->next;
        tmp->next = n1->next->next;
        n1->next->next = tmp;
        n1 = n1->next->next;
    }
    return tmp_h->next;
}

int main(void)
{
    printf("#24. 两两交换链表中的节点\n");

    struct ListNode *head = generate_list(3);
    print_list(head);
    head = swapPairs(head);
    print_list(head);

    return 0;
}